package com.maori.languageapp.subview;

import com.maori.languageapp.LanguageAppCaptureActivity;
import com.maori.languageapp.R;
import com.maori.languageapp.consts.CaptureFilterAdapter;
import com.maori.languageapp.discover.Maori_Discover_Word;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Maori_BookMark_Items extends Activity {
	private static final String TAG = null;
	private CaptureFilterAdapter dbHelper;
	private SimpleCursorAdapter dataAdapter;
	ListView listViewitemslist;
	Button btnsearchclose;
	EditText searchfiltertext;
	String type, BMNID, CATEGORY;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.language_app_search_area);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			CATEGORY = extras.getString("CATEGORY");
			BMNID = extras.getString("BMNID");
		}
		
		listViewitemslist = (ListView) findViewById(R.id.listViewitemslist);
		btnsearchclose = (Button) findViewById(R.id.btnsearchclose);
		searchfiltertext = (EditText) findViewById(R.id.searchfiltertext);
		
		dbHelper = new CaptureFilterAdapter(this);
		dbHelper.open();
		
		btnsearchclose.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				onBackPressed();
			}
		});
	
		loadWordsDetailsList();
		
	}
	
	// *********Word List View Populating*************
		private void loadWordsDetailsList() {
			// TODO Auto-generated method stub
			Cursor cursor = dbHelper.fetchAllBookMarkItems();
			// The desired columns to be bound
			String[] columns = new String[] { CaptureFilterAdapter.KEY_ENG_TITLE,
					CaptureFilterAdapter.KEY_MAO_TITLE, CaptureFilterAdapter.KEY_NID };
			String[] fromtrdes = new String[] { "coldes_1", "coldes_2" };
			// the XML defined views which the data will be bound to
			int[] to = new int[] { R.id.discover_maori_title,
					R.id.discover_english_title };
			// create the adapter using the cursor pointing to the desired data
			// as well as the layout information
			dataAdapter = new SimpleCursorAdapter(this,
					R.layout.language_capture_filter_list_raw, cursor, columns, to,
					0);

			ListView listView = (ListView) findViewById(R.id.listViewitemslist);
			// Assign adapter to ListView
			listView.setAdapter(dataAdapter);

			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> listView, View view,
						int position, long id) {
					// Get the cursor, positioned to the corresponding row in the
					// result set
					Cursor cursor = (Cursor) listView.getItemAtPosition(position);

					// Get the state's capital from this row in the database.
					String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("NID"));
					Intent myIntent = new Intent(Maori_BookMark_Items.this,
							Maori_Node_Detail_View.class);
					myIntent.putExtra("REFNID",countryCode);
					startActivity(myIntent);
				}
			});

			searchfiltertext.addTextChangedListener(new TextWatcher() {

				public void afterTextChanged(Editable s) {
				}

				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
				}

				public void onTextChanged(CharSequence s, int start, int before,
						int count) {
					dataAdapter.getFilter().filter(s.toString());
				}
			});

			dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
				public Cursor runQuery(CharSequence constraint) {
					return dbHelper.fetchBookMarkItemsByName(constraint.toString());
				}
			});
		}
}
